package com.qboot.generate.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.date.DateUtil;
import com.qboot.common.annotation.Log;
import com.qboot.common.consts.MsgConst;
import com.qboot.common.controller.BaseController;
import com.qboot.common.entity.Result;
import com.qboot.common.entity.TableResult;
import com.qboot.common.utils.EasyPoiExcelUtils;
import com.qboot.generate.entity.GenModule;
import com.qboot.generate.service.GenModuleService;
import com.qboot.generate.vo.GenModuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 模块
 *
 * @author Tellsea
 * @date 2023-02-08
 */
@Api(tags = "模块")
@RestController
@RequestMapping("/generate/genModule")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class GenModuleController extends BaseController {

    private final GenModuleService genModuleService;

    @ApiOperation("模块查询")
    @Log("模块查询")
    @SaCheckPermission("generate:genModule:list")
    @GetMapping("/list")
    public TableResult<GenModuleVo> queryList(GenModuleVo entity) {
        return TableResult.ok(genModuleService.queryList(getAllPage(), entity));
    }

    @ApiOperation("模块导入")
    @Log("模块导入")
    @SaCheckPermission("generate:genModule:import")
    @PostMapping("/importExcel")
    public Result importExcel(@RequestParam("file") MultipartFile file) {
        List<GenModule> list = EasyPoiExcelUtils.importExcel(file, 1, 1, GenModule.class);
        genModuleService.saveBatch(list, list.size());
        return Result.ok(MsgConst.IMPORT_SUCCESS + list.size() + "条数据");
    }

    @ApiOperation("模块导出")
    @Log("模块导出")
    @SaCheckPermission("generate:genModule:export")
    @GetMapping("/exportExcel")
    public void exportExcel(GenModuleVo entity) {
        List<GenModuleVo> list = genModuleService.queryList(getAllPage(), entity).getRecords();
        EasyPoiExcelUtils.exportExcel(list, "模块-" + DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss"), GenModuleVo.class);
    }

    @ApiOperation("模块根据主键查询")
    @Log("模块根据主键查询")
    @SaCheckPermission("generate:genModule:get")
    @GetMapping("/queryById/{id}")
    public Result<GenModuleVo> queryById(@PathVariable("id") Long id) {
        return queryResult(genModuleService.queryById(id));
    }

    @ApiOperation("模块新增")
    @Log("模块新增")
    @SaCheckPermission("generate:genModule:save")
    @PostMapping("/save")
    public Result save(@RequestBody GenModuleVo entity) {
        return saveResult(genModuleService.save(entity));
    }

    @ApiOperation("模块根据主键更新")
    @Log("模块根据主键更新")
    @SaCheckPermission("generate:genModule:update")
    @PostMapping("/updateById")
    public Result updateById(@RequestBody GenModuleVo entity) {
        return updateResult(genModuleService.updateById(entity));
    }

    @ApiOperation("模块根据主键删除")
    @Log("模块根据主键删除")
    @SaCheckPermission("generate:genModule:remove")
    @GetMapping("/removeById/{id}")
    public Result removeById(@PathVariable("id") Long id) {
        return removeResult(genModuleService.removeById(id));
    }

    @ApiOperation("模块根据主键批量删除")
    @Log("模块根据主键批量删除")
    @SaCheckPermission("generate:genModule:removeBatch")
    @GetMapping("/removeBatchByIds/{ids}")
    public Result removeBatchByIds(@PathVariable("ids") List<Long> ids) {
        return removeResult(genModuleService.removeBatchByIds(ids));
    }
}
